//
//  BHReportDetailViewController.swift
//  BigHealth
//
//  Created by 蔺百好 on 2021/6/15.
//

import UIKit

class BHReportDetailViewController: BHBaseViewController {

    lazy var tableView: UITableView = {
        let view = UITableView.buildTableView(style: .plain)
        view.frame = CGRect(x: 0, y: UIScreen.begainY, width: UIScreen.width, height: UIScreen.showHeight)
        view.delegate = self
        view.dataSource = self
        return view
    }()
    var model: BHCompetitionReportModel!
    
    lazy var contentWebCell: BHHtmlContentWebCell = { [weak self] in
        let cell = self?.tableView.dequeueReusableCell(withIdentifier: "BHHtmlContentWebCell", for: IndexPath.init(row: 0, section: 1)) as! BHHtmlContentWebCell
        return cell
    }()
    
    fileprivate var titleHeight: CGFloat = 0
    fileprivate var contentHeight: CGFloat = 0
    
    override func viewDidLoad() {
        super.viewDidLoad()

        self.bh_navigationBar.title_label.text = "报道详情"
        
        self.regisetCell()
        self.view.addSubview(self.tableView)
        
        self.configContent()
    }
    
    
    private func configContent() {
        
        let tH = self.model.title.calSizeOfText(font: .systemFont(ofSize: 14, weight: .bold), size: CGSize(width: UIScreen.width-32, height: CGFloat.greatestFiniteMagnitude)).height
        self.titleHeight = tH+60;
        
        self.tableView.reloadData()
        
        self.contentWebCell.htmlText = self.model.content;
        self.contentWebCell.webCellCallRealHeightBlock = {[weak self] height in
            self?.contentHeight = height
            
            self?.tableView.reloadData()
        }
        
    }
    


}
extension BHReportDetailViewController {
    func regisetCell()  {
        self.tableView.register(UINib.init(nibName: "BHReportDetailTitleCell", bundle: nil), forCellReuseIdentifier: "BHReportDetailTitleCell")
        self.tableView.register(BHHtmlContentWebCell.self, forCellReuseIdentifier: "BHHtmlContentWebCell")

    }
}

extension BHReportDetailViewController: UITableViewDataSource {
    
    func numberOfSections(in tableView: UITableView) -> Int {
        return 2
    }
    func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
        return 1
    }
    func tableView(_ tableView: UITableView, heightForRowAt indexPath: IndexPath) -> CGFloat {
        if indexPath.section == 0 {
            return self.titleHeight
        }else {
            return self.contentHeight
        }
    }
    
}

extension BHReportDetailViewController: UITableViewDelegate {
 
    func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
        if indexPath.section == 0 {
            let cell = tableView.dequeueReusableCell(withIdentifier: "BHReportDetailTitleCell", for: indexPath) as! BHReportDetailTitleCell
            cell.title_label.text = self.model.title
            cell.time_label.text = self.model.createTime
            return cell
        }else {
            return self.contentWebCell
        }
    }
}

